package com.projectName.www.service;


import com.projectName.www.util.String_util;
import com.projectName.www.util.find;
import com.projectName.www.dao.S_J_updatadishes_dao;

import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.ResultSet;
import java.util.Vector;

/**
     * @author 86132
     *
     */
    public class S_J_updatadishes extends JInternalFrame {
        private JTable table;
        private JTextField finddishe;
        private JTextField newname;
        private JTextField newmonkey;
        private  JTextArea newdes;
        /**
         * Launch the application.
         */
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        S_J_updatadishes frame = new S_J_updatadishes();
                        frame.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }

        /**
         * Create the frame.
         */
        public S_J_updatadishes() throws Exception {
            setMaximizable(true);
            setClosable(true);
            setBounds(100, 100, 645, 491);

            JScrollPane scrollPane = new JScrollPane();

            finddishe = new JTextField();
            finddishe.setColumns(10);

            JLabel lblNewLabel = new JLabel("输入菜名：");

            JButton select = new JButton("查询");
            select.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    //查询
                   if(String_util.isEmpty(finddishe.getText()))
                   {
                       JOptionPane.showMessageDialog(null, "查询失败");
                       return;
                   }
                   else {
                       try {
                           s_dish(e);
                       } catch (Exception ex) {
                           throw new RuntimeException(ex);
                       }
                   }

                }


            });

            JPanel panel = new JPanel();
            GroupLayout groupLayout = new GroupLayout(getContentPane());
            groupLayout.setHorizontalGroup(
                    groupLayout.createParallelGroup(Alignment.LEADING)
                            .addGroup(groupLayout.createSequentialGroup()
                                    .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                            .addGroup(groupLayout.createSequentialGroup()
                                                    .addGap(143)
                                                    .addComponent(lblNewLabel)
                                                    .addPreferredGap(ComponentPlacement.UNRELATED)
                                                    .addComponent(finddishe, GroupLayout.PREFERRED_SIZE, 231, GroupLayout.PREFERRED_SIZE)
                                                    .addGap(37)
                                                    .addComponent(select))
                                            .addGroup(groupLayout.createSequentialGroup()
                                                    .addGap(74)
                                                    .addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 437, GroupLayout.PREFERRED_SIZE))
                                            .addGroup(groupLayout.createSequentialGroup()
                                                    .addGap(20)
                                                    .addComponent(panel, GroupLayout.PREFERRED_SIZE, 597, GroupLayout.PREFERRED_SIZE)))
                                    .addContainerGap(18, Short.MAX_VALUE))
            );
            groupLayout.setVerticalGroup(
                    groupLayout.createParallelGroup(Alignment.TRAILING)
                            .addGroup(Alignment.LEADING, groupLayout.createSequentialGroup()
                                    .addGap(18)
                                    .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                                            .addComponent(finddishe, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE)
                                            .addComponent(lblNewLabel)
                                            .addComponent(select))
                                    .addPreferredGap(ComponentPlacement.RELATED)
                                    .addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 108, GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(ComponentPlacement.RELATED, 34, Short.MAX_VALUE)
                                    .addComponent(panel, GroupLayout.PREFERRED_SIZE, 261, GroupLayout.PREFERRED_SIZE)
                                    .addContainerGap())
            );

            JButton updata = new JButton("修改");
            updata.addActionListener(e -> {
                //修改
                try {
                    updishes(e);
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
            });

            JButton close = new JButton("取消");
            close.addActionListener(e -> {
                //取消修改
                try {
                    notupdata(e);
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
            });

            JLabel lblNewLabel_1 = new JLabel("修改菜品");

            JLabel lblNewLabel_2 = new JLabel("菜名：");

            newname = new JTextField();
            newname.setEditable(false);
            newname.setColumns(10);

            JLabel lblNewLabel_3 = new JLabel("新描述");

             newdes = new JTextArea();

            JLabel lblNewLabel_3_1 = new JLabel("新价格：");

            newmonkey = new JTextField();
            newmonkey.setColumns(10);
            GroupLayout gl_panel = new GroupLayout(panel);
            gl_panel.setHorizontalGroup(
                    gl_panel.createParallelGroup(Alignment.LEADING)
                            .addGroup(gl_panel.createSequentialGroup()
                                    .addGap(271)
                                    .addComponent(lblNewLabel_1)
                                    .addContainerGap(278, Short.MAX_VALUE))
                            .addGroup(gl_panel.createSequentialGroup()
                                    .addGroup(gl_panel.createParallelGroup(Alignment.LEADING, false)
                                            .addGroup(gl_panel.createSequentialGroup()
                                                    .addGap(119)
                                                    .addComponent(updata, GroupLayout.PREFERRED_SIZE, 118, GroupLayout.PREFERRED_SIZE))
                                            .addGroup(gl_panel.createSequentialGroup()
                                                    .addGap(50)
                                                    .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
                                                            .addGroup(gl_panel.createSequentialGroup()
                                                                    .addComponent(lblNewLabel_3)
                                                                    .addPreferredGap(ComponentPlacement.UNRELATED)
                                                                    .addComponent(newdes, GroupLayout.PREFERRED_SIZE, 164, GroupLayout.PREFERRED_SIZE))
                                                            .addGroup(gl_panel.createSequentialGroup()
                                                                    .addComponent(lblNewLabel_2)
                                                                    .addPreferredGap(ComponentPlacement.RELATED)
                                                                    .addComponent(newname)))))
                                    .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
                                            .addGroup(gl_panel.createSequentialGroup()
                                                    .addPreferredGap(ComponentPlacement.RELATED, 144, Short.MAX_VALUE)
                                                    .addComponent(close, GroupLayout.PREFERRED_SIZE, 118, GroupLayout.PREFERRED_SIZE)
                                                    .addGap(75))
                                            .addGroup(gl_panel.createSequentialGroup()
                                                    .addGap(36)
                                                    .addComponent(lblNewLabel_3_1)
                                                    .addGap(18)
                                                    .addComponent(newmonkey, GroupLayout.PREFERRED_SIZE, 81, GroupLayout.PREFERRED_SIZE)
                                                    .addContainerGap())))
            );
            gl_panel.setVerticalGroup(
                    gl_panel.createParallelGroup(Alignment.LEADING)
                            .addGroup(gl_panel.createSequentialGroup()
                                    .addGap(19)
                                    .addComponent(lblNewLabel_1)
                                    .addGap(17)
                                    .addGroup(gl_panel.createParallelGroup(Alignment.TRAILING)
                                            .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
                                                    .addComponent(lblNewLabel_2)
                                                    .addComponent(newname, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                            .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
                                                    .addComponent(lblNewLabel_3_1)
                                                    .addComponent(newmonkey, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
                                    .addGap(29)
                                    .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
                                            .addComponent(lblNewLabel_3)
                                            .addComponent(newdes, GroupLayout.PREFERRED_SIZE, 66, GroupLayout.PREFERRED_SIZE))
                                    .addPreferredGap(ComponentPlacement.RELATED, 40, Short.MAX_VALUE)
                                    .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
                                            .addComponent(close, GroupLayout.PREFERRED_SIZE, 54, GroupLayout.PREFERRED_SIZE)
                                            .addComponent(updata, GroupLayout.PREFERRED_SIZE, 54, GroupLayout.PREFERRED_SIZE)))
            );
            panel.setLayout(gl_panel);

            table = new JTable();
            table.addMouseListener(new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent e) {
                    //按表编辑
                    try {
                        updata(e);
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }

                }
            });
            table.setModel(new DefaultTableModel(
                    new Object[][] {
                            {null, null, null},
                            {null, null, null},
                            {null, null, null},
                            {null, null, null},
                            {null, null, null},
                    },
                    new String[] {
                            "\u83DC\u540D", "\u63CF\u8FF0", "\u4EF7\u683C"
                    }
            ));
            table.getColumnModel().getColumn(1).setPreferredWidth(249);
            table.getColumnModel().getColumn(2).setPreferredWidth(65);
            scrollPane.setViewportView(table);
            getContentPane().setLayout(groupLayout);
            this.filltable();

        }
    //取消修改
    private void notupdata(ActionEvent e) {
            newdes.setText("");
            newmonkey.setText("");
    }


    //修改
    private void updishes(ActionEvent e) throws Exception {
        if(S_J_updatadishes_dao.updatadishe(newname.getText(),newdes.getText(),newmonkey.getText()))
        { JOptionPane.showMessageDialog(null, "修改成功");
            return;}
        else  JOptionPane.showMessageDialog(null, "修改成功");
        return;
    }

    //修改区文本填充
    private void updata(MouseEvent e) throws Exception {
        ResultSet dishes = find.dishes(finddishe.getText());
        dishes.next();
        newname.setText(dishes.getString("name"));
        newdes.setText(dishes.getString("des"));
        newmonkey.setText(dishes.getString("money"));
    }
    //预查询
    private void filltable() throws Exception {
            DefaultTableModel model = (DefaultTableModel) table.getModel();
            model.setRowCount(0);//设置为0行
            ResultSet f = S_J_updatadishes_dao.fill();
            while (f.next())
            {
                Vector v=new Vector();
                v.add(f.getString("name"));
                v.add(f.getString("des"));
                v.add(f.getString("money"));
                model.addRow(v);
        }

        }
        //查询
    private void s_dish(ActionEvent e) throws Exception {
        DefaultTableModel model = (DefaultTableModel) table.getModel();
        model.setRowCount(0);//设置为0行
        ResultSet dishes = find.dishes(finddishe.getText());
        while(dishes.next())
        {
            Vector v=new Vector();
            v.add(dishes.getString("name"));
            v.add(dishes.getString("des"));
            v.add(dishes.getString("money"));
            model.addRow(v);
        }

    }
}
